package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;

import model.Model;
import model.entity.Talkable;

public class TalkOverlay extends Overlay {
	
	private final int boxWidth = 500;
	private final int boxHeight = 200;
	private final int spacingX = 10;
	private final int spacingY = 30;

	private Talkable talkableEntity;
	private int currentSpeech;
	private boolean isDone;
	private ArrayList<String> dialogue;
	
	public TalkOverlay(Dimension size, Talkable talkableEntity) {
		super(size);
		this.talkableEntity = talkableEntity;
		isDone = false;
		this.dialogue = new ArrayList<String>(); 
		for (String s : talkableEntity.getDialog() ) {
			this.dialogue.add( s );
		}
		this.currentSpeech = 0;
	}
	
	@Override
	protected void drawOverlay(Graphics g) {
		Point upperLeft = new Point( 0, super.getOuterDimension().height/2 - boxHeight/2 );
		Color oldC = g.getColor();
		g.setColor( Color.WHITE );
		g.fillRect( upperLeft.x, upperLeft.y , boxWidth, boxHeight);
		g.setColor( oldC );
		if( isDone() ) {
			this.drawString( "Done. Press your escape key to exit.", g, upperLeft);
		} else {//is not done. Draw the string
			if ( currentSpeech >= dialogue.size() ) {
				this.setDone();
				return ;
			}
			String p = dialogue.get( currentSpeech );
			drawString( p, g, upperLeft );
		}
	}
	
	private void drawString( String s, Graphics g, Point upperLeft) {
		g.drawString( s, upperLeft.x + spacingX, upperLeft.y + spacingY );
	}

	@Override
	protected void onResize() {}

	@Override
	public void update(Model m) {}
	
	public void advanceToNextLine() {
		currentSpeech++;
		super.updateSubscribers();
	}
	
	public boolean isDone() {
		return isDone;
	}
	
	private void setDone() {
		isDone = true;
		super.updateSubscribers();
	}
	
}
